using System;
using Sce.PlayStation.Core;

namespace Nyanimals
{
	public class CollisionSphere : CollisionShape
	{
		private Vector3 position;
		private float radius;
		
#if SHOWCOLLISION
		CStaticContext sphereContext;
        public CStaticContext Context
        {
            get { return sphereContext;}
            set { sphereContext = value;}
        }
#endif
		
		public override Vector3 Position
		{
			get { return position;}
			set 
			{ 
				position = value;
#if SHOWCOLLISION
				sphereContext.WorldMatrix = BuildMatrix();		
#endif
			}
		}
		public float Radius
		{
			get { return radius;}
			set 
			{
				radius = value;
#if SHOWCOLLISION
				sphereContext.WorldMatrix = BuildMatrix();	
#endif
			}
		}
		public override bool Draw
		{
			get { return bDraw;}
			set 
            {
                bDraw = value;
                
#if SHOWCOLLISION
                sphereContext.Active = value;
#endif
            }
		}
		
		private Matrix4 BuildMatrix()
		{
			Matrix4 mat = Matrix4.Translation(position);
			Matrix4 scl = Matrix4.Scale(radius, radius, radius);
			
			return mat * scl;
		}
		
         public CollisionSphere(Vector3 pos, float rad)
         {
             position = pos;
             radius = rad;
             
#if SHOWCOLLISION
             // load in the sphere mesh
             int meshIndex = CMeshManager.Instance.LoadMesh("debug_sphere.SRM");         
             int contextIndex = CContextManager.Instance.AddStaticContext(meshIndex, BuildMatrix());
            
             sphereContext = (CStaticContext)CContextManager.Instance.GetContext(contextIndex);
             
             sphereContext.Active = bDraw;           
             sphereContext.WireFrame = true;
#endif
         }
         ~CollisionSphere()
         {
            Clear();
         }
		
        public override void Clear()
        {
            
#if SHOWCOLLISION
            if(sphereContext != null)
            {
                CContextManager.Instance.RemoveContext(sphereContext);
                sphereContext = null;
            }
#endif
        }
        
		public override CollisionTypes Type 
		{
			get { return CollisionTypes.SPHERE;}
		}
		
		public override void Render ()
		{
		}

		public override bool Collides (ref CollisionShape other)
		{
			switch(other.Type)
			{
				case CollisionTypes.SPHERE:
				{
				} break;				
			};
			
			return false;
		}
		
		
		private bool SphereToSphereCollisionCheck(ref CollisionSphere other)
		{
			
			return false;
		}
	}
}

